package com.lzs.baseruku.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.lzs.Listener.MyExcelListener;
import com.lzs.baseruku.service.CommodityService;
import com.lzs.baseruku.service.WarehouseService;
import com.lzs.entity.Commodity;
import com.lzs.entity.Warehouse;
import com.lzs.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author ymx
 * @since 2020-11-23
 */
@RestController
@RequestMapping("/baseruku/commodity")
@Api(value = "商品管理文档")
public class CommodityController {
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private WarehouseService warehouseService;

    @ApiOperation(value = "下拉查询所有仓库")
    @GetMapping("/selectWarehouse")
    public R selectWarehouse(){
        List<Warehouse> warehouses = this.warehouseService.list();
        return R.ok().data("warehouses",warehouses);
    }

    @ApiOperation(value = "根据商品条码查询商品信息")
    @GetMapping("/selectCommodityByBar/{bar}")
    public Commodity selectCommodityByBar(@PathVariable String bar){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("bar",bar);
        return this.commodityService.getOne(queryWrapper);
    }
    @ApiOperation(value = "查询所有的商品")
    @PostMapping("/findCommodity/{page}/{limit}")
    public R findCommodity(@PathVariable Integer page, @PathVariable Integer limit, @RequestBody Commodity commodity){
        Page<Commodity> commoditys= commodityService.findCommodity(page, limit, commodity);
        return R.ok().data("commoditys",commoditys);
    }

    @ApiOperation(value = "新增或修改商品")
    @PostMapping("/insertOrUpdateCommodity")
    public R insertOrUpdateCommodity(@RequestBody Commodity commodity){
        Long warehouseid=commodity.getWarehouseid();
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("id",warehouseid);
        Warehouse warehouse = this.warehouseService.getOne(queryWrapper);
        String name=warehouse.getName();
        commodity.setWarehousename(name);
        boolean b = commodityService.saveOrUpdate(commodity);
        if (b){
            return R.ok().message("操作成功");
        }else {
            return R.error().message("操作失败");
        }
    }

    @ApiOperation(value = "根据ID查询商品")
    @GetMapping("/findCommodityById/{id}")
    public R findCommodityById(@PathVariable Long id){
        Commodity commodity = this.commodityService.getById(id);
        return R.ok().data("commodity",commodity);
    }

    @ApiOperation(value = "根据ID删除商品")
    @DeleteMapping("/deleteCommodity/{id}")
    public R deleteCommodity(@PathVariable Long id){
        boolean b = this.commodityService.removeById(id);
        if (b){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }
    }

    @ApiOperation(value = "导出商品表")
    @GetMapping("/daochu")
    public R daochu(){
        try {
            List<Commodity> list = commodityService.list();
            EasyExcel.write("F://commodityinfo.xls", Commodity.class).sheet("商品信息").doWrite(list);
            return R.ok().message("导出成功");
        }catch (Exception e){
            e.printStackTrace();
            return R.error().message("导出失败");
        }
    }

//    @ApiOperation(value = "导入商品表")
//    @GetMapping("/daoru")
//    public R daoru() {
//        try {
//            EasyExcel.read("F://commodityinfo.xls", Commodity.class,new MyExcelListener<Commodity>()).sheet("商品信息").doRead();
//            return R.ok().message("导入成功");
//        }catch (Exception e){
//            e.printStackTrace();
//            return R.error().message("导入失败");
//        }
//    }
}

